package work5;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

public class stop
{

    private static Map<String, Integer> myTreeMap = null;
    private static List<Map.Entry<String, Integer>> list = null;

    public static void main(String[] args) {
        
        int n = 0;
        Scanner in = new Scanner(System.in);
        String filename = "D:\\\\one.txt"; 
  
            System.out.println("停用词 ");
   
                    readAndSave(filename);
                    sort();
                    stop();
                    for(Map.Entry<String, Integer> map : list) {
                        if((!map.getKey().equals("the"))||(!map.getKey().equals("a"))||(map.getKey().equals("this"))||(!map.getKey().equals("it"))||(!map.getKey().equals("and")))
                            System.out.println(map.getKey() + " :\t" + map.getValue());
                    }//停用"a",  "it", "the", "and", "this"
                 
                }


 
    public static void readAndSave(String filename)
    {
        //long t1 = System.currentTimeMillis();
        String s;
        BufferedReader br = null;
        
        try
        {
            br = new BufferedReader(new FileReader(filename));
            
            StringBuffer sb = new StringBuffer(); 
            //将文件内容存入StringBuffer中  
            while((s = br.readLine()) != null) {  
                sb.append(s);  
            }
            //不区分大小写
            String str = sb.toString().toLowerCase();
            //分隔字符串并存入数组  (以，。空格分割)
            String[] elements = str.split("[,.“” ‘’?\\s]");  
            int count = 0;  
            myTreeMap = new TreeMap<String, Integer>();  
            //遍历数组将其存入Map<String, Integer>中  
            for(int i = 0; i < elements.length; i++) {  
                if(myTreeMap.containsKey(elements[i])) {  
                    count = myTreeMap.get(elements[i]);  
                    myTreeMap.put(elements[i], count + 1);  
                 }  
                 else {  
                    myTreeMap.put(elements[i], 1);  
                 }  
            }
          //直接遍历Map输出
            /*for(Map.Entry<String, Integer> entry:myTreeMap.entrySet()){
                System.out.println(entry.getKey()+" :\t"+entry.getValue());
            }*/
            //对频率从大到小排序，然后输出
            
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally
        {
            try
            {
                br.close();
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    public static void sort()
    {
        //将map.entrySet()转换成list  
        list = new ArrayList<Map.Entry<String, Integer>>(myTreeMap.entrySet());  
        //通过比较器实现排序  
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {  
            //降序排序  
            public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {  
                return o2.getValue().compareTo(o1.getValue());  
//                return o1.getValue().compareTo(o2.getValue()); //升序排序
            }  
        });  
    }
    
   
  
  
    public static void stop()
    {
        String filename_stop = "\"D:\\stopwords_en.txt\"";
        String str = null;
        try {
            BufferedReader br_stop = new BufferedReader(new FileReader(filename_stop));
             ArrayList<String> stopword = new ArrayList();
                while ((str = br_stop.readLine()) != null) {//使用readLine方法，一次读一行 读取停用词
                    stopword.add(str);
                }
                br_stop.close();
                myTreeMap.remove(stopword);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}